<?php

namespace app\command;


use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
use app\common\lib\MsgQueue as MQ;
use app\service\MessageQueue as MsgService;

class MessageQueue extends Command
{
    protected function configure()
    {
        $this->setName('MQ')->setDescription('消息队列消费者');
    }


    protected function execute(Input $input, Output $output)
    {
        try {
            $m = new MQ();
            $callback = function ($msg) {
                $queueName = $msg->delivery_info['routing_key'];
                $msgData = json_decode($msg->body, true);
                if ($msgData) {
                    $msgObj = new MsgService();
                    if (!$msgObj->receiveCallBack($queueName, $msgData['body'])) {
                        $m = new MQ();
                        $m->sendMessage($queueName, $msgData['body']);
                    }
                }
                $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
            };
            $m->receiveMesage([
                MQ::QUEUE_NORMAL,
                MQ::QUEUE_OUTAPI_ACCESSLOG,
                MQ::QUEUE_RESPONSE_LOG,
                MQ::QUEUE_FILE_UPLOAD,
            ], $callback);
        } catch (\Exception $e) {

        }
    }
}